<?php
//by willson 105328468@qq.com 2014/10/11
//index page interface

class ad_model extends base_model {

	public function __construct() {
		parent::__construct();
	}

	//获取详情
	public function detail($id) {
		$sql = "SELECT * FROM {$this->tbp}ad WHERE id='{$id}'";
		$row = $this->db->get_row($sql);
		return $row;
	}
	
	//类型
	public function types() {
		$sql = "SELECT * FROM {$this->tbp}ad_type WHERE status=1";
		$type = $this->db->get_results($sql);
		return $type;
	}

	//类型名称
	public function type_name($type) {
		$types = $this->types();
		foreach ($types as $k=>$g) {
			if ($g->name == $type) {
				return $g->value;
				break;
			}
		}
		return '';
	}
	
	//位置
	public function positions($has_categorys=false) {
		$sql = "SELECT * FROM {$this->tbp}ad_position WHERE status=1";
		$position = $this->db->get_results($sql);
		if ($has_categorys) {
			$sql = "SELECT id, name FROM {$this->tbp}goods_category WHERE status=1 AND parent_id=0 ORDER BY sort DESC, id ASC";
			$categorys = $this->db->get_results($sql);
			if ($categorys) {
				foreach ($categorys as $k=>$g) {
					$data = new stdClass();
					$data->name = "category{$g->id}";
					$data->value = "分类 - {$g->name}(category{$g->id})";
					$data->memo = '';
					$position[] = $data;
				}
			}
		}
		return $position;
	}
	
	//位置名称
	public function position_name($position) {
		if (stripos($position, 'category')!==false) {
			$p = str_replace('category', '', $position);
			return $position.'(分类 - '.$this->_category_name($p).')';
		}
		$positions = $this->positions();
		foreach ($positions as $k=>$g) {
			if ($g->name == $position) {
				return $g->value;
				break;
			}
		}
		return '';
	}
	//分类名称
	private function _category_name($id) {
		$sql = "SELECT name FROM {$this->tbp}goods_category WHERE id={$id}";
		return $this->db->get_var($sql);
	}
}
